package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    //关联mapper
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if(strategyCatalog.getSequence()==null){
            //去数据库查询当前分类下已经有多少数据
            int max=strategyCatalogMapper.selectMaxSequence(strategyCatalog.getStrategy().getId());
            strategyCatalog.setSequence(max+1);
        }
        //id为空,新增操作
        if(strategyCatalog.getId()==null){
            //新增操作时如果没填序列,我们给他设置一个
            strategyCatalogMapper.insert(strategyCatalog);
        }else{
            strategyCatalogMapper.updateByPrimaryKey(strategyCatalog);
        }
    }

    @Override
    public PageInfo<StrategyCatalog> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyCatalog> list = strategyCatalogMapper.selectForList(qo);
        return new PageInfo<StrategyCatalog>(list);
    }

    @Override
    public List listByStrategyId(Long strategyId) {
        return strategyCatalogMapper.selectByStrategyId(strategyId);
    }

}
